<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/8 0008
 * Time: 16:16
 */

namespace app\api\middleware;


use app\currency\service\AccessTokenService;
use library\Controller;
use think\Db;

/**
 * 保洁员登陆中间件
 * Class CheckCleaner
 * @package app\api\middleware
 */
class CleanerAuth extends Controller
{
    /**
     * 保洁员登陆验证中间件
     * @param $request
     * @param \Closure $next
     * @param string $name
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function handle($request, \Closure $next, $name = '')
    {
        // 接受token
        $token = $this->request->header('Authorization',input('server.HTTP_AUTHORIZATION',''));
        $token = trim(substr($token, 6));

        $cid = AccessTokenService::checkAccessToken($token,'cleaner');
        if(is_array($cid) && $name !== 'compatible'){
            $this->error($cid['msg'], '', $cid['code']);
        }
        $cleaner = Db::name('Cleaner')->where('id',$cid)->find();
        if($cleaner['phone'] == '' && $name != 'compatible' && $request->controller != 'home')
        {
            $this->success('您还未绑定手机号！',$request->controller,202);
        }
        $request->cleaner = (object)$cleaner;
        $request->cid = $cid;
        return $next($request);
    }
}